package com.audi.box.controller;

import java.util.ArrayList;

import android.content.Context;

import com.audi.box.adapter.SQLiteAsusHelperPlace;
import com.audi.box.service.PlaceService;
import com.audi.box.service.PlaceServiceImp;
import com.audi.dto.PlaceItem;

/**
 * @author CuongPH7
 * 
 */
public class PlaceController implements IController {
    private PlaceService placeService;
    
    private static Object pclObj = new Object();
    private HistoryController historyController;
    private static PlaceController placeController = null;
    
    /**
     * Class constructor specifying Context to create
     * 
     * @param android
     *            .content.Context context
     * @throws Exception
     *             if (can not create PlaceServiceImp
     */
    public PlaceController(Context context) throws Exception {
        placeService = new PlaceServiceImp(new SQLiteAsusHelperPlace(context));
        historyController = HistoryController.getInstance(context);
    }
    
    /**
     * Class constructor specifying PlaceService to create
     * 
     * @param placeService
     *            PlaceService
     * @see PlaceService
     */
    public PlaceController(PlaceService placeService) {
        this.placeService = placeService;
    }
    
    /**
     * Make a Singleton Instance.
     * 
     * @param context
     *            Android.content.Context
     * @return an Instance PlaceController
     * @throws Exception
     */
    public static PlaceController getInstance(final Context context)
            throws Exception {
        if (placeController == null) {
            synchronized (pclObj) {
                if (placeController == null) {
                    placeController = new PlaceController(context);
                }
            }
        }
        
        return placeController;
    }
    
    /**
     * Inject PlaceService
     * 
     * @param placeService
     *            PlaceService
     * @see PlaceService
     */
    public void setPlaceService(PlaceService placeService) {
        this.placeService = placeService;
    }
    
    /**
     * Add a new place by PlaceItem
     * 
     * @param place
     *            PlaceItem
     * @return Place Id if (successful, 0 if (place is exist
     * @see PlaceItem
     */
    public int addPlace(PlaceItem place) {
        if (place == null) {
            throw new NullPointerException("Place is Null");
        }
        if (place.getPlaceName() == null || place.getPlaceName().equals("")
                || place.getCapabilityType() == null
                || place.getCapabilityType() == 0) {
            throw new NullPointerException("Place Information is missing");
        } else if (place.getPlaceId() != null && place.getPlaceId() != 0
                && isExist(place)) {
            return 0;
        } else {
            return placeService.addPlace(place);
        }
    }
    
    /**
     * Add a new place by place information
     * 
     * @param place
     *            PlaceItem
     * @return Place Id if (successful, 0 if (place is exist
     * @see PlaceItem
     */
    public int addPlace(String placeName, String placeIcon, int placeCapability) {
        
        PlaceItem place = new PlaceItem();
        place.setPlaceName(placeName);
        place.setPlaceImage(placeIcon);
        place.setCapabilityType(placeCapability);
        
        return addPlace(place);
        
    }
    
    /**
     * Change Place's Capability
     * 
     * @param place
     *            PlaceItem
     * @return 1 if successful, 0 if place is not exist
     */
    public int changeCapability(PlaceItem place) {
        if (place == null) {
            throw new NullPointerException("Place is Null");
        }
        
        if (place.getPlaceId() == 0 || place.getCapabilityType() == null) {
            throw new NullPointerException("Place Information is missing");
        }
        
        return updatePlace(place);
    }
    
    /**
     * Rename a Place
     * 
     * @param place
     *            PlaceItem
     * @return 1 if (successful, 0 if (place is not exist
     */
    public int renamePlace(PlaceItem place) {
        if (place == null) {
            throw new NullPointerException("Place is Null");
        }
        
        if (place.getPlaceId() == 0 || place.getPlaceName() == null
                || place.getPlaceName().equals("")) {
            throw new NullPointerException("Place Information is missing");
        }
        
        if (placeService.isExistByName(place)) {
            return 0;
        }
        
        return updatePlace(place);
        
    }
    
    /**
     * Check Place Exist
     * 
     * @param place
     *            PlaceItem
     * @return 1 if (successful, 0 if (place is not exist
     */
    public boolean isExist(PlaceItem place) {
        if (place == null) {
            throw new NullPointerException("Place is Null");
        }
        
        return placeService.isExist(place);
    }
    
    /**
     * Check Place's Name
     * 
     * @param place
     *            PlaceItem
     * @return 1 if (successful, 0 if (place is not exist
     */
    public boolean isExistByName(PlaceItem place) {
        if (place == null) {
            throw new NullPointerException("Place is Null");
        }
        
        return placeService.isExistByName(place);
    }
    
    /**
     * Delete a Place
     * 
     * @param place
     *            PlaceItem
     * @return 1 if (successful, 0 if (place is not exist
     */
    public int deletePlace(PlaceItem place) {
        if (place == null) {
            throw new NullPointerException("Place is Null");
        }
        
        if (place.getPlaceId() == 0) {
            throw new NullPointerException("Place Information is missing");
        }
        if (!placeService.isExist(place)) {
            return 0;
        } else {
            return placeService.deletePlace(place);
        }
    }
    
    /**
     * Get All Place List
     * 
     * @return An ArrayList Place
     */
    public ArrayList<PlaceItem> getPlaceList() {
        return placeService.getListPlace();
    }
    
    /**
     * Get Place List By User
     * 
     * @param userId
     *            User Id
     * @return An ArrayList Place
     */
    public ArrayList<PlaceItem> getPlaceListByUser(int userId) {
        return placeService.getListPlaceByUserId(userId);
    }
    
    /**
     * Get Place Item
     * 
     * @param placeId
     *            Place id
     * @return PlaceItem
     */
    public PlaceItem getPlace(int placeId) {
        return placeService.getPlace(placeId);
    }
    
    /**
     * Update place Item
     * <p>
     * Add 06/09
     * 
     * @since 06.09
     * @param place
     *            PlaceItem
     * @return 1 if (successful, 0 if (place is not exist
     */
    public int updatePlace(PlaceItem place) {
        if (place == null) {
            throw new NullPointerException("Place is Null");
        }
        
        if (!placeService.isExist(place)) {
            return 0;
        }
        
        return placeService.updatePlace(place);
    }
}
